﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using Dictionary.ERP;
using Common;
using System.Drawing;
using DevExpress.XtraEditors.Controls;
using Extra.ERP;
using Security.ERP;

namespace Dictionary
{
    public class xucAllowanceAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_ALLOWANCE item);

        private readonly IContainer components = null;

        private CalcEdit calIncomeTaxValue;
        private CalcEdit calMaximumSalary;
        private CheckEdit cheIsByWorkDay;
        private CheckEdit cheIsShowInSalaryTableList;
        private LabelControl labelControl1;
        private LabelControl lcDescription;
        private LabelControl lcMaximumMoney;
        private TextEdit txtDescription;

        public xucAllowanceAdd()
        {
            InitializeComponent();
            InitMultiLanguages();
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new DIC_ALLOWANCE().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucAllowanceAdd));
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            calMaximumSalary = new CalcEdit();
            lcMaximumMoney = new LabelControl();
            calIncomeTaxValue = new CalcEdit();
            labelControl1 = new LabelControl();
            cheIsShowInSalaryTableList = new CheckEdit();
            cheIsByWorkDay = new CheckEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtDescription.Properties.BeginInit();
            calMaximumSalary.Properties.BeginInit();
            calIncomeTaxValue.Properties.BeginInit();
            cheIsShowInSalaryTableList.Properties.BeginInit();
            cheIsByWorkDay.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(cheIsByWorkDay);
            base.gcInfo.Controls.Add(cheIsShowInSalaryTableList);
            base.gcInfo.Controls.Add(labelControl1);
            base.gcInfo.Controls.Add(lcMaximumMoney);
            base.gcInfo.Controls.Add(calIncomeTaxValue);
            base.gcInfo.Controls.Add(calMaximumSalary);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Size = new Size(0x14c, 0xe0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(calMaximumSalary, 0);
            base.gcInfo.Controls.SetChildIndex(calIncomeTaxValue, 0);
            base.gcInfo.Controls.SetChildIndex(lcMaximumMoney, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl1, 0);
            base.gcInfo.Controls.SetChildIndex(cheIsShowInSalaryTableList, 0);
            base.gcInfo.Controls.SetChildIndex(cheIsByWorkDay, 0);
            base.txtNAME.Location = new Point(0x62, 0x33);
            base.txtNAME.Size = new Size(0xde, 20);
            base.lblName.Location = new Point(6, 0x34);
            base.lblName.Size = new Size(0x3f, 13);
            base.lblName.Text = "T\x00ean phụ cấp:";
            base.txtID.Location = new Point(0x62, 0x19);
            base.txtID.Size = new Size(0xde, 20);
            base.lblId.Location = new Point(6, 0x1b);
            base.lblId.Size = new Size(0x3b, 14);
            base.lblId.Text = "M\x00e3 phụ cấp:";
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x23, 0xea);
            base.btnCancel.Location = new Point(0xe2, 0xea);
            base.btnSaveNew.Location = new Point(0x85, 0xea);
            txtDescription.Location = new Point(0x62, 0x84);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xde, 20);
            txtDescription.TabIndex = 15;
            lcDescription.Location = new Point(8, 0x86);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 0x10;
            lcDescription.Text = "Ghi ch\x00fa:";
            calMaximumSalary.Location = new Point(0x62, 0x4d);
            calMaximumSalary.Name = "calMaximumSalary";
            calMaximumSalary.Properties.Buttons.AddRange(new EditorButton[] {new EditorButton(ButtonPredefines.Combo)});
            calMaximumSalary.Properties.DisplayFormat.FormatString = "{0:##,##0}";
            calMaximumSalary.Properties.EditFormat.FormatString = "{0:##,##0}";
            calMaximumSalary.Properties.Mask.UseMaskAsDisplayFormat = true;
            calMaximumSalary.Size = new Size(0xde, 20);
            calMaximumSalary.TabIndex = 0x11;
            lcMaximumMoney.Location = new Point(8, 0x4f);
            lcMaximumMoney.Name = "lcMaximumMoney";
            lcMaximumMoney.Size = new Size(0x45, 13);
            lcMaximumMoney.TabIndex = 0x12;
            lcMaximumMoney.Text = "Số tiền chuẩn:";
            calIncomeTaxValue.Location = new Point(0x62, 0x68);
            calIncomeTaxValue.Name = "calIncomeTaxValue";
            calIncomeTaxValue.Properties.Buttons.AddRange(new EditorButton[] {new EditorButton(ButtonPredefines.Combo)});
            calIncomeTaxValue.Properties.DisplayFormat.FormatString = "{0:##,##0}";
            calIncomeTaxValue.Properties.EditFormat.FormatString = "{0:##,##0}";
            calIncomeTaxValue.Properties.Mask.UseMaskAsDisplayFormat = true;
            calIncomeTaxValue.Size = new Size(0xde, 20);
            calIncomeTaxValue.TabIndex = 0x11;
            labelControl1.Location = new Point(8, 0x6a);
            labelControl1.Name = "labelControl1";
            labelControl1.Size = new Size(0x54, 13);
            labelControl1.TabIndex = 0x12;
            labelControl1.Text = "Giảm trừ thuế %:";
            cheIsShowInSalaryTableList.Location = new Point(6, 0xb9);
            cheIsShowInSalaryTableList.Name = "cheIsShowInSalaryTableList";
            cheIsShowInSalaryTableList.Properties.Appearance.Options.UseTextOptions = true;
            cheIsShowInSalaryTableList.Properties.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            cheIsShowInSalaryTableList.Properties.Caption =
                "Hiển thị giảm trừ thuế cho phụ cấp n\x00e0y trong bảng lương th\x00e1ng";
            cheIsShowInSalaryTableList.Size = new Size(0x13a, 0x1f);
            cheIsShowInSalaryTableList.TabIndex = 0x13;
            cheIsByWorkDay.Location = new Point(6, 0xa1);
            cheIsByWorkDay.Name = "cheIsByWorkDay";
            cheIsByWorkDay.Properties.Appearance.Options.UseTextOptions = true;
            cheIsByWorkDay.Properties.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            cheIsByWorkDay.Properties.Caption = "Phụ thuộc ng\x00e0y c\x00f4ng";
            cheIsByWorkDay.Size = new Size(0x13a, 0x13);
            cheIsByWorkDay.TabIndex = 0x13;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucAllowanceAdd";
            base.Size = new Size(0x14d, 0x10d);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtDescription.Properties.EndInit();
            calMaximumSalary.Properties.EndInit();
            calIncomeTaxValue.Properties.EndInit();
            cheIsShowInSalaryTableList.Properties.EndInit();
            cheIsByWorkDay.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            base.lblId.Text = MultiLanguages.GetString("tbl_DicAllowanceAdd", "AllowanceCode", base.lblId.Text);
            base.lblName.Text = MultiLanguages.GetString("tbl_DicAllowanceAdd", "AllowanceName", base.lblName.Text);
            lcMaximumMoney.Text = MultiLanguages.GetString("tbl_DicAllowanceAdd", "MaximumMoney", lcMaximumMoney.Text);
            lcDescription.Text = MultiLanguages.GetString("tbl_DicAllowanceAdd", "Description", lcDescription.Text);
        }

        private void RaiseSuccessEventHander(DIC_ALLOWANCE item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_ALLOWANCE item)
        {
            base.txtID.Text = item.AllowanceCode;
            SYS_LOG.Insert("Danh Mục Phụ Cấp", "Xem", base.txtID.Text);
            if (base.m_Status == Common.Class.Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.AllowanceName;
            calMaximumSalary.EditValue = item.MaximumMoney;
            cheIsByWorkDay.Checked = item.IsByWorkDay;
            calIncomeTaxValue.EditValue = item.IncomeTaxValue;
            cheIsShowInSalaryTableList.Checked = item.IsShowInSalaryTableList;
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Common.Class.Actions.Add)
            {
                var dic_allowance = new DIC_ALLOWANCE();
                if (dic_allowance.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_allowance = new DIC_ALLOWANCE();
                if ((base.m_Status == Common.Class.Actions.Add) && dic_allowance.Exist(control.Text))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_ALLOWANCE
            {
                AllowanceCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiAllowance") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Phụ Cấp", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_ALLOWANCE(base.txtID.Text, base.txtNAME.Text,
                Convert.ToDecimal(calMaximumSalary.EditValue), cheIsByWorkDay.Checked,
                Convert.ToDouble(calIncomeTaxValue.EditValue), cheIsShowInSalaryTableList.Checked, txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiAllowance") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Phụ Cấp", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var dic_allowance = new DIC_ALLOWANCE();
            dic_allowance.Get(base.txtID.Text);
            var item = new DIC_ALLOWANCE(base.txtID.Text, base.txtNAME.Text,
                Convert.ToDecimal(calMaximumSalary.EditValue), cheIsByWorkDay.Checked,
                Convert.ToDouble(calIncomeTaxValue.EditValue), cheIsShowInSalaryTableList.Checked, txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
                if ((dic_allowance.IsByWorkDay != item.IsByWorkDay) ||
                    (dic_allowance.IncomeTaxValue != item.IncomeTaxValue))
                {
                    if (
                        XtraMessageBox.Show(
                            "Bạn c\x00f3 muốn thay đổi th\x00f4ng tin phụ cấp của tất cả nh\x00e2n vi\x00ean kh\x00f4ng?",
                            "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        this.DoHide();
                        return "";
                    }
                    item.UpdateEmployeeAllowance(item.AllowanceCode, cheIsByWorkDay.Checked,
                        Convert.ToDouble(calIncomeTaxValue.EditValue));
                }
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}